﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Garlic.BLL;
using Garlic.CC.BaseClass;
using Garlic.CC.WebUtility;
using Garlic.DM.DataModel;
using System.Collections.Generic;
using Garlic.CC.Utility;

public partial class SystemManagement_SetMenuForAction : BasePage
{
    

    private List<MenuInfo> forActionMenus;

    #region page load
    protected void Page_Load(object sender, EventArgs e)
    {
        //searchParentMenu.Text = hdParent_menu_name.Value;
        try
        {
            //WorkshopMgr = new WorkshopManager(this.CurrentUser);
            if (!Page.IsPostBack)
            {
                //Init Page
                //   InitSearchBar();

                string actionID = Request.QueryString["ActionID"];
                if (string.IsNullOrEmpty(actionID))
                    throw new Exception();

                ActionInfo action = ActionManager.GetInfo(int.Parse(actionID));

                actionName.InnerText = action.Action_name;
                hdActionID.Value = actionID;

                //string parentMenuID = Request.QueryString["parentMenuID"];
                //string parentMenuName = Request.QueryString["parentMenuName"];
                //if (!string.IsNullOrEmpty(parentMenuID))
                //{
                //    hdParent_menu_id.Value = parentMenuID;
                //    hdParent_menu_name.Value = parentMenuName;
                //    searchParentMenu.Text = parentMenuName;
                //}

                //this.Search();

                MenuManager.BindMenuList(this.searchParentMenu);
                string parentMenuID = Request.QueryString["parentMenuID"];
                string parentMenuName = Request.QueryString["parentMenuName"];
                if (!string.IsNullOrEmpty(parentMenuID))
                {
                    this.searchParentMenu.SelectedValue = parentMenuID;
                    //this.Search();
                }
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }

    #endregion

    #region method
    /// <summary>
    /// 初始化页面
    /// </summary>
    private void InitPage()
    {

    }

    /// <summary>
    /// 初始化查询栏
    /// </summary>
    private void InitSearchBar()
    {
        //初始化查询列表
        // this.GeneralSearchBar1.ListSourcXYbles = WorkshopMgr.GetSearchBarDataSourcXYbles();


    }

    private void SaveRecords()
    {
        List<int> checkedRecords = new List<int>();
        List<int> allRecords = new List<int>();
        for (int i = 0; i < this.SmartGrid.Rows.Count; i++)
        {
            CheckBox cbSelected = this.SmartGrid.Rows[i].FindControl("checkitem") as CheckBox;
            if (cbSelected != null)
            {
                if (cbSelected.Checked)
                    checkedRecords.Add(int.Parse(SmartGrid.DataKeys[i].Value.ToString()));
                allRecords.Add(int.Parse(SmartGrid.DataKeys[i].Value.ToString()));
            }
        }
        //if (checkedRecords.Count == 0) return;

        int actionID = int.Parse(hdActionID.Value);

        foreach (int menuID in allRecords)
        {
            ActionManager.RemoveToMenu(actionID, menuID);
        }

        foreach (int menuID in checkedRecords)
        {
            ActionManager.AddToMenu(actionID, menuID);
        }

        this.ShowMessage("成功。");
    }

    private void Search()
    {
        //string condition = this.GeneralSearchBar1.GetConditionStr();
        string condition = "";

        if (searchMenuName.Text.Trim() != "")
            condition += " menu_name like '%" + SqlHelper.ReplaceInvalidWords(searchMenuName.Text) + "%'";

        if (!string.IsNullOrEmpty(this.searchParentMenu.SelectedValue))
        {
            if (condition != "")
                condition += " and ";
            condition += " parent_menu_id = " + searchParentMenu.SelectedValue;
        }

        this.SmartDataViewSource.SelectParameters["textWhere"].DefaultValue = condition;
    }

    private void ShowMessage(string msg)
    {
        this.Message = msg;
    }

    #endregion

    #region toolbar event handler
    /// <summary>
    /// 工具栏事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="args"></param>
    protected void TopToolBar_OnClick(object sender, Garlic.UI.Components.IconButtonClickEventArgs args)
    {
       
            switch (args.Command)
            {
                case ToolbarCommand.Save:
                    SaveRecords();
                    break;
                case ToolbarCommand.Search:
                    Search();
                    break;
                case ToolbarCommand.HideOrExpand:
                    this.divSearch.Visible = !this.divSearch.Visible;
                    break;
            }
       
    }
    #endregion


    protected void SmartGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[2].Text == "0")
                    e.Row.Cells[2].Text = "系统";
                else if (e.Row.Cells[2].Text != "")
                {
                    MenuInfo menu = MenuManager.GetInfo(int.Parse(e.Row.Cells[2].Text));
                    e.Row.Cells[2].Text = menu.Menu_name;
                    MenuInfo parentMenu = MenuManager.GetInfo(menu.Parent_menu_id);
                    if (parentMenu == null)
                    {
                        parentMenu = new MenuInfo();
                        parentMenu.Menu_id = 0;
                        parentMenu.Menu_name = "系统";
                    }
                    e.Row.Cells[2].Text += " (<a href='SetMenuForAction.aspx?parentMenuID=" + parentMenu.Menu_id.ToString() + "&parentMenuName=" + parentMenu.Menu_name + "&ActionID=" + hdActionID.Value + "'>查看上级菜单</a>)";
                }
                //e.Row.Cells[1].Text = searchMenuName.Text;

                if (forActionMenus == null)
                {
                    forActionMenus = MenuManager.GetMenusByActionID(int.Parse(hdActionID.Value));
                }

                foreach (MenuInfo menu in forActionMenus)
                {
                    if (int.Parse(SmartGrid.DataKeys[e.Row.DataItemIndex - SmartGrid.PageSize * (SmartGrid.PageIndex)].Value.ToString()) == menu.Menu_id)
                    {
                        //CheckBox cbSelected = this.SmartGrid.Rows[e.Row.DataItemIndex].FindControl("checkitem") as CheckBox;
                        CheckBox cbSelected = e.Row.FindControl("checkitem") as CheckBox;
                        if (cbSelected != null)
                        {
                            cbSelected.Checked = true;
                        }
                        break;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }
}
